package testview.zhen.com.myapplication


import java.lang.ref.WeakReference
import java.util.concurrent.ConcurrentHashMap

/**
 * Create by ldr
 * on 2019/11/11 16:54.
 */
class AllTestMain {
    companion object {
        var local = LocalThreadExp<String>()
        @JvmStatic
        fun main(args: Array<String>) {
            local.set("main()")
            //线程A
            var threadA = Thread(Runnable {
                local.set("线程A")
                println(local.get())
            })
            //线程B
            var threadB = Thread(Runnable {
                local.set("线程B")
                println(local.get())
            })
            println(local.get())
            Thread.sleep(500)
            threadA.start()
            Thread.sleep(500)
            threadB.start()
            Thread.sleep(500)
            println(local.get())
        }
    }
}
class LocalThreadExp<T>() {

    var mHashMap = ConcurrentHashMap<Thread, T>()

    fun set(value: T) {
        mHashMap[Thread.currentThread()] = value
    }

    fun get(): T? {
        val thread = Thread.currentThread()
        var  o = mHashMap[thread]
        return o
    }
}